<?php
/**
 * @desc   Created by PhpStorm.
 * @author laowng
 * @since  2018/8/17 14:49
 */

namespace app\admin\common;

use app\admin\model\LoginLogModel;

class UserFunction
{
    /**登录日志
     * @param $userType
     * @param $userId
     * @param $type
     * @param $from
     * @param $result
     * @return bool
     */
    public function addLoginLog($userType, $userId, $type, $result = 2)
    {

        $data['user_type'] = $userType;//用户类型：1管理|2用户
        $data['user_id'] = $userId;
        $data['type'] = $type;//日志类型 1登录|2退出
        # $data['browser'] = userBrowser();//用户访问浏览器类型
        # $data['system'] = userOS();//用户访问操作系统类型
        $data['result'] = $result;//登录结果 2失败 1成功
        $data['create_at'] = date('Y-m-d H:i:s', time());
        $data['create_ip'] = request()->ip();
        LoginLogModel::add($data);

    }

}

/**
 * 查询访问用户的操作系统
 * @return string
 */
function userOS()
{
    $os = "";
    $Agent = \Request::param('server.HTTP_USER_AGENT');
    if (preg_match('/win/i', $Agent) && strpos($Agent, '95')) {
        $os = "Windows 95";
    } elseif (preg_match('/win 9x/i', $Agent) && strpos($Agent, '4.90')) {
        $os = "Windows ME";
    } elseif (preg_match('/win/i', $Agent) && preg_match('/98/i', $Agent)) {
        $os = "Windows 98";
    } elseif (preg_match('/win/i', $Agent) && preg_match('/nt 5.0/i', $Agent)) {
        $os = "Windows 2000";
    } elseif (preg_match('/win/i', $Agent) && preg_match('/nt/i', $Agent)) {
        $os = "Windows NT";
    } elseif (preg_match('/win/i', $Agent) && preg_match('/nt 5.1/i', $Agent)) {
        $os = "Windows XP";
    } elseif (preg_match('/win/i', $Agent) && strpos('32', $Agent)) {
        $os = "Windows 32";
    } elseif (preg_match('/linux/i', $Agent)) {
        $os = "Linux";
    } elseif (preg_match('/unix/i', $Agent)) {
        $os = "Unix";
    } elseif (preg_match('/sun/i', $Agent) && preg_match('/os/i', $Agent)) {
        $os = "SunOS";
    } elseif (preg_match('/ibm/i', $Agent) && preg_match('/os/i', $Agent)) {
        $os = "IBM OS/2";
    } elseif (preg_match('/Mac/i', $Agent) && preg_match('/PC/i', $Agent)) {
        $os = "Macintosh";
    } elseif (preg_match('/PowerPC/i', $Agent)) {
        $os = "PowerPC";
    } elseif (preg_match('/AIX/i', $Agent)) {
        $os = "AIX";
    } elseif (preg_match('/HPUX/i', $Agent)) {
        $os = "HPUX";
    } elseif (preg_match('/NetBSD/i', $Agent)) {
        $os = "NetBSD";
    } elseif (preg_match('/BSD/', $Agent)) {
        $os = "BSD";
    } elseif (preg_match('/OSF1/i', $Agent)) {
        $os = "OSF1";
    } elseif (preg_match('/IRIX/i', $Agent)) {
        $os = "IRIX";
    } elseif (preg_match('/FreeBSD/i', $Agent)) {
        $os = "FreeBSD";
    }
    if ($os == '') $os = "Unknown";
    return $os;
}

